package com.ruoyi.ftrl.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ftrl.domain.BuildingInfo;
import com.ruoyi.ftrl.service.IBuildingInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 楼栋信息Controller
 *
 * @author dihang
 * @date 2024-07-08
 */
@RestController
@RequestMapping("/ftrl/building")
public class BuildingInfoController extends BaseController {
    @Autowired
    private IBuildingInfoService buildingInfoService;

    /**
     * 根据小区id获取楼栋集合
     */
//    @PreAuthorize("@ss.hasPermi('ftrl:building:query')")
    @GetMapping(value = "/building/{id}")
    public TableDataInfo getChildInfo(@PathVariable("id") Long id) {
        List<BuildingInfo> buildingInfos = buildingInfoService.selectBuildingInfoByParentId(id);
        return getDataTable(buildingInfos) ;
    }


    /**
     * 查询楼栋信息列表
     */
    @PreAuthorize("@ss.hasPermi('ftrl:building:list')")
    @GetMapping("/list")
    public TableDataInfo list(BuildingInfo buildingInfo) {
        startPage();
        List<BuildingInfo> list = buildingInfoService.selectBuildingInfoList(buildingInfo);
        return getDataTable(list);
    }

    /**
     * 导出楼栋信息列表
     */
    @PreAuthorize("@ss.hasPermi('ftrl:building:export')")
    @Log(title = "楼栋信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuildingInfo buildingInfo) {
        List<BuildingInfo> list = buildingInfoService.selectBuildingInfoList(buildingInfo);
        ExcelUtil<BuildingInfo> util = new ExcelUtil<BuildingInfo>(BuildingInfo.class);
        util.exportExcel(response, list, "楼栋信息数据");
    }

    /**
     * 获取楼栋信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('ftrl:building:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(buildingInfoService.selectBuildingInfoById(id));
    }

    /**
     * 新增楼栋信息
     */
    @PreAuthorize("@ss.hasPermi('ftrl:building:add')")
    @Log(title = "楼栋信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuildingInfo buildingInfo) {
        return toAjax(buildingInfoService.insertBuildingInfo(buildingInfo));
    }

    /**
     * 修改楼栋信息
     */
    @PreAuthorize("@ss.hasPermi('ftrl:building:edit')")
    @Log(title = "楼栋信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuildingInfo buildingInfo) {
        return toAjax(buildingInfoService.updateBuildingInfo(buildingInfo));
    }

    /**
     * 删除楼栋信息
     */
    @PreAuthorize("@ss.hasPermi('ftrl:building:remove')")
    @Log(title = "楼栋信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(buildingInfoService.deleteBuildingInfoByIds(ids));
    }
}
